package com.chenyun.gateway.filter;//package com.chenyun.gateway.filter;
//
//import com.chenyun.gateway.util.ResponseUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.checkerframework.checker.units.qual.A;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.text.ParseException;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @descriptions: 全局过滤器的使用 案例:如果不存在token 直接返回错误信息
// * @author: Lonni
// * @date: 2020/9/9 22:09
// * @version: 1.0
// */
//@Component
//@Slf4j
//public class TokenFilter implements GlobalFilter, Ordered {
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        String token = exchange.getRequest().getHeaders().getFirst("Authorization");
//        // 如果token不存在 放行交由下异步处理
//        if (StringUtils.isEmpty(token)) {
//            return chain.filter(exchange);
//        }
////        try {
////            //判断是否在黑名单中
////            SignedJWT signedJWT = SignedJWT.parse(token);
////            JWTClaimsSet jwtClaimsSet = signedJWT.getJWTClaimsSet();
////            // JWT唯一标识
////            String jti =jwtClaimsSet.getJWTID();
////            Boolean hasBlockKey = redisTemplate.hasKey("auth:token:blacklist:" + jti);
////            if (hasBlockKey){
////                //存在hi名单中 拦截
////                Map<String, Object> map = new HashMap<String, Object>() {{
////                    put("code", 3003);
////                    put("message", "对不起,无效凭据,请重新登录!");
////                    put("success", false);
////                    put("data", null);
////                }};
////                return ResponseUtil.wirteResponse(exchange, map);
////            }
////
////            String realToken = token.replace("Bearer ", "");
////            JWSObject jwsObject = JWSObject.parse(realToken);
////            String userStr = jwsObject.getPayload().toString();
////            log.info("AuthGlobalFilter.filter() user:{}",userStr);
////            // 从token中解析用户信息并设置到Header中去 传递给微服务
////            ServerHttpRequest request = exchange.getRequest().mutate().header("payload", userStr).build();
////            exchange = exchange.mutate().request(request).build();
////        } catch (ParseException e) {
////            log.info("解析token信息失败:{}",e.getMessage());
////            e.printStackTrace();
////        }
//        return chain.filter(exchange);
//
//    }
//
//    /**
//     * 过滤器执行的顺序 值越小  越前执行
//     *
//     * @return
//     */
//    @Override
//    public int getOrder() {
//        return -1;
//    }
//}
